#! /bin/bash -xv

{# This is the shell script that launcher vms for large_scale_boot benchmark
uses to spin up vms. The vms are spun up with a startup script to curl a
listening server. The time taken to boot is the time between executing this
script and the time the vms call back on the server. #}

{% if contact_launcher %}
  LINUX_PING="curl -H 'X-Header: {{ launcher_vm_name }}' http://{{ server_ip }}:{{ server_port }}"
  WINDOWS_PING="Invoke-WebRequest -Uri '{{ server_ip }}:{{ server_port }}' -Headers @{'X-Header'='{{ launcher_vm_name }}'}"
{% else %}
  LINUX_PING=""
  WINDOWS_PING=""
{% endif %}

{% if cloud == 'GCP' %}
  LINUX_START_UP_SCRIPT_PARAM="startup-script="
  WINDOWS_START_UP_SCRIPT_PARAM="windows-startup-script-ps1="
  WINDOWS_START_UP_SCRIPT_CLOSING_PARAM=""
  DELETE_VM="{{ gcloud_path }} --quiet compute instances delete {{ boot_vm_name_prefix }}-$i --zone {{ zone }} --project {{ project }}
/sbin/shutdown -hP now"
{% elif cloud == 'AWS' %}
  LINUX_START_UP_SCRIPT_PARAM=""
  WINDOWS_START_UP_SCRIPT_PARAM="<powershell>"
  WINDOWS_START_UP_SCRIPT_CLOSING_PARAM="</powershell>"
  INSTANCE_ID="`wget -q -O - http://169.254.169.254/latest/meta-data/instance-id`"
  DELETE_VM="aws ec2 terminate-instances --region {{ region }} --instance-ids ${INSTANCE_ID}"
{% elif cloud == 'Azure' %}
  LINUX_START_UP_SCRIPT_PARAM=""
  WINDOWS_START_UP_SCRIPT_PARAM=""
  WINDOWS_START_UP_SCRIPT_CLOSING_PARAM=""
  DELETE_VM=""
{% endif %}

export LINUX_SLEEP="# Sleep 10min before final cleanup and VM termination.
sleep {{ timeout }}"
export WINDOWS_SLEEP="# Sleep 10min before final cleanup and VM termination.
Start-Sleep -Seconds {{ timeout }}"

{% if os_type == 'linux' %}
  export START_UP_SCRIPT="${LINUX_START_UP_SCRIPT_PARAM}#! /bin/bash
  ${LINUX_PING}
  ${LINUX_SLEEP}
  ${DELETE_VM}"
{% elif os_type == 'windows' %}
  export START_UP_SCRIPT="${WINDOWS_START_UP_SCRIPT_PARAM}
  ${WINDOWS_PING}
  ${WINDOWS_SLEEP}
  ${DELETE_VM}
  ${WINDOWS_START_UP_SCRIPT_CLOSING_PARAM}"
{% endif %}

# Launcher VM record the system nanoseconds as start time.
echo $(date +%s%N) > {{start_time_file}}

# Launch some VMs
{% if cloud == 'GCP' %}
  gcloud_args=(
    --format text
    --quiet
    --project {{ project }}
    --scopes compute-rw
    --image-family {{ image_family }}
    --image-project {{ image_project }}
    --boot-disk-auto-delete
    --boot-disk-size {{ boot_disk_size }}
    --boot-disk-type pd-ssd
    --machine-type {{ boot_machine_type }}
    --tags perfkitbenchmarker
    --no-restart-on-failure
    --zone {{ zone }}
    --metadata "${START_UP_SCRIPT}"
  )
  {% if os_type == 'linux' %}
    gcloud_args+=( --network-interface {{ use_public_ip }}address,network-tier=PREMIUM )
  {% endif %}
  {% if use_public_ip != '' %}
    gcloud_args+=( --async )
  {% endif %}
  VM_NAMES=`seq -f '{{ boot_vm_name_prefix }}-%g' -s ' ' 1 {{ vm_count }}`
  {{ gcloud_path }} compute instances create $VM_NAMES  \
  "${gcloud_args[@]}" &
{% elif cloud == 'AWS' %}
  aws_args=(
    --output text
    --region {{ region }}
    --subnet-id {{ subnet_id }}
    --image-id {{ image }}
    --key-name {{ key_name }}
    --instance-type {{ boot_machine_type }}
    --{{ use_public_ip }}associate-public-ip-address
    --tag-specifications 'ResourceType=instance,Tags=[{Key=launcher_id,Value={{launcher_vm_name}}}]'
    --placement AvailabilityZone={{ zone }},GroupName={{ group_name }}
    --user-data "${START_UP_SCRIPT}"
    --count {{vm_count}}
  )
  aws ec2 run-instances "${aws_args[@]}" &
  sleep 3
  aws --output text ec2 describe-instances --region {{ region }} --filter Name=tag:launcher_id,Values={{ launcher_vm_name }}
{% elif cloud == 'Azure' %}
  azure_args=(
    --location {{ location }}
    --image {{ image }}
    --size {{ boot_machine_type }}
    --admin-username perfkit
    --storage-sku {{ storage_sku }}
    --resource-group {{ resource_group }}
    --custom-data "${START_UP_SCRIPT}"
    --generate-ssh-keys
    --no-wait
  )
  {% if os_type == 'windows' %}
    azure_args+=( --admin-password '{{ password }}' )
  {% endif %}
  ((end_id={{ start_id }}+{{ vm_count }}-1))
  for i in $(seq {{start_id}} "${end_id}"); do
    az vm create --name {{ boot_vm_name_prefix }}-"${i}"  \
    --nics {{ nic }}"${i}" \
    "${azure_args[@]}" &
  done
{% endif %}

wait
echo [completed]
